package problems.practice;

/**
 * 59.螺旋矩阵II
 * https://leetcode.cn/problems/spiral-matrix-ii/
 *
 * @author habitplus
 * @since 2022/8/3 19:37
 */
public class T59 {
    public int[][] generateMatrix(int n) {
        if (n < 1) return new int[0][];

        int circle = (n + 1) / 2;
        int[][] ans = new int[n][n];
        int it = 1;
        for (int c = 1; c <= circle; c++) {
            // 上 水平 往右
            for (int i = c - 1; i <= n - c; i++) ans[c - 1][i] = it++;
            // 右 垂直 往下
            for (int i = c; i <= n - c; i++) ans[i][n - c] = it++;
            // 下 水平 往左
            for (int i = n - c - 1; i >= c - 1; i--) ans[n - c][i] = it++;
            // 左 垂直 往上
            for (int i = n - c - 1; i > c - 1; i--) ans[i][c - 1] = it++;
        }

        return ans;

    }
}
